package produce.ttlMessage;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : xiaohong
 * @date : 2021/4/11 15:35
 */

@RestController
@RequestMapping("/ttlMessage")
public class TTLMessageController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping
    public String send(@RequestParam String msg){

        // 自定义消息属性
        MessageProperties messageProperties = new MessageProperties();
        // 给消息设置过期时间
        messageProperties.setExpiration("5000");

        // 创建消息
        Message message = new Message(msg.getBytes(), messageProperties);

        // 交换机名称  路由key/queue队列名称  消息内容
        rabbitTemplate.convertAndSend("ttlMessage_exchange","ttlMessage",message);
        return "发送给ttlMessage消息成功:  "+msg;
    }

}
